//
//  XHMainViewController.swift
//  swift新浪微博
//
//  Created by 魏兴浩 on 15/11/24.
//  Copyright © 2015年 wxh. All rights reserved.
//

import UIKit

class XHMainViewController: UITabBarController {

    func composeClick(){
        
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置自定义tabBar--使用kvc
        let newTabBar = XHTabBar()
        setValue(newTabBar, forKey: "tabBar")
        /**
        撰写按钮的点击事件
        
        - parameter action:           执行方法
        - parameter forControlEvents: 控件事件的描述
        */
        newTabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)

        tabBar.tintColor = UIColor.orangeColor()
        
        //创建主页
        let homeVC = XHHomeViewController()
        addChildViewController(homeVC, title: "首页", imagename: "tabbar_home")

        //创建消息
        let messageVc = XHMessageViewController()
        addChildViewController(messageVc, title: "消息", imagename: "tabbar_message_center")
        
        //创建发现
        let discoverVc = XHDiscoverViewController()
        addChildViewController(discoverVc, title: "发现", imagename: "tabbar_discover")
        
        //我
        let profileVC = XHProfileViewController()
        addChildViewController(profileVC, title: "我", imagename: "tabbar_profile")
    }

    //代码抽取简化方法
      private func addChildViewController(controller: UIViewController,title: String,imagename: String){
        //设置tiele
          controller.title=title
        //设置image
          controller.tabBarItem.image=UIImage(named: imagename)
        
        //把控制器添加到UINavigationController中
        addChildViewController(UINavigationController(rootViewController: controller))


    }
}
    


